package com.boot.security.server.controller.system;

import com.boot.security.server.dao.mffl.MfflTypeDao;
import com.boot.security.server.dao.mffl.MfflUserDao;
import com.boot.security.server.model.mffl.MfflUser;
import com.boot.security.server.model.mffl.Vo.LawyerUser;
import com.boot.security.server.page.table.PageTableHandler;
import com.boot.security.server.page.table.PageTableHandler.CountHandler;
import com.boot.security.server.page.table.PageTableHandler.ListHandler;
import com.boot.security.server.page.table.PageTableRequest;
import com.boot.security.server.page.table.PageTableResponse;
import com.boot.security.server.utils.mffl.ReturnBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "后台律师审核")
@RestController
@RequestMapping("/lawyerAudit")
public class MfflLawyerAuditController {

    @Autowired
    private MfflUserDao mfflUserDao;

    @Autowired
    private MfflTypeDao mfflTypeDao;


    @GetMapping("/{id}")
    @ApiOperation(value = "根据id获取")
    public MfflUser get(@PathVariable Long id) {
        MfflUser mfflUser = mfflUserDao.getById(id);
        return getField(mfflUser);
    }


    @PostMapping("/audit")
    @ApiOperation(value = "修改")
    public ReturnBean audit(@ApiParam(value = "id", name = "id") @RequestParam(required = false) Long id,
                            @ApiParam(value = "type", name = "type") @RequestParam(required = false) Integer type) {
        //通过
        int status = 0;
        if (type == 1 ){
            status = this.mfflUserDao.auditLawyer(id,type);
        }else {
            status = this.mfflUserDao.auditLawyer(id,type);
        }
        if (status == 0 ){
            return new ReturnBean("201","审核失败");
        }
        return new ReturnBean();
    }

    @GetMapping
    @ApiOperation(value = "列表")
    public PageTableResponse list(PageTableRequest request) {
        return new PageTableHandler(new CountHandler() {

            @Override
            public int count(PageTableRequest request) {
                return mfflUserDao.lawyerAuditCount(request.getParams());
            }
        }, new ListHandler() {

            @Override
            public List<MfflUser> list(PageTableRequest request) {
                return mfflUserDao.lawyerAuditList(request.getParams(), request.getOffset(), request.getLimit());
            }
        }).handle(request);
    }

    private MfflUser getField(MfflUser mfflUser) {
            if (!StringUtils.isEmpty(mfflUser.getField())) {
                String[] typeGroup = mfflUser.getField().split(",");
                String fields = "";
                for (String field : typeGroup) {
                    if (field != null && !"".equals(field)) {
                        fields += field + ",";
                    }
                }
                List<String> list = this.mfflTypeDao.getLawyerTypeListById(fields.substring(0, fields.length() - 1));
                mfflUser.setField(String.join(",",list));
            }
        return mfflUser;
    }
}

